// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Mobage.java

package com.sgame.android;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.*;

import com.sgame.android.network.Credentials;
import com.sgame.android.network.ISgameHttpClientFactory;
import com.sgame.android.network.ISgameHttpClient;
import com.sgame.android.lang.Error;
import com.sgame.android.lang.ErrorMap;
import com.sgame.android.lang.Resource;
import com.sgame.android.data.SessionData;
import com.sgame.android.data.User;
import com.sgame.android.social.CommonAPIBase;
import com.sgame.android.social.Connection;
import com.sgame.android.social.IMobageHttpResponseHandler;
import com.sgame.android.social.InvalidConfigurationException;
import com.sgame.android.social.InvalidParameterException;
import com.sgame.android.social.Service;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;

public class Mobage extends CommonAPIBase
{
    public static interface OnLogoutComplete
    {

        public abstract void onSuccess();

        public abstract void onCancel();
    }

    public static interface PlatformListener
    {

        public abstract void onLoginComplete(String s);

        public abstract void onLoginRequired();

        public abstract void onLoginError(Error error);

        public abstract void onLoginCancel();

        public abstract void onSplashComplete();
    }

    public static enum MarketCode
    {
        MOBAGE, GOOGLE_ANDROID_MARKET;

        public static MarketCode fromString(String value)
        {
            if(value == null || value.trim().length() == 0)
                return MOBAGE;
            String v = value.trim().toLowerCase();
            if(v.equals("amkt"))
                return GOOGLE_ANDROID_MARKET;
            if(v.equals("mbga"))
            {
                return MOBAGE;
            } else
            {
                MLog.w("Mobage", (new StringBuilder()).append("unknown market code:").append(value).toString());
                return MOBAGE;
            }
        }

        public String toString()
        {
            switch(ordinal())
            {
            case 1: // '\001'
                return "mbga";

            case 2: // '\002'
                return "amkt";
            }
            return "mbga";
        }

    }


    public static enum Region
    {
        US, JP, CN;

        public static Region fromString(String value)
        {
            Region arr$[] = values();
            int len$ = arr$.length;
            for(int i$ = 0; i$ < len$; i$++)
            {
                Region region = arr$[i$];
                if(value.equals(region.toString()))
                    return region;
            }

            MLog.e("Mobage", (new StringBuilder()).append("unknown region:").append(value).toString());
            return JP;
        }

        public String toString()
        {
            switch(ordinal())
            {
            case 1: // '\001'
                return "US";

            case 2: // '\002'
                return "JP";

            case 3: // '\003'
                return "CN";
            }
            MLog.e("Mobage", (new StringBuilder()).append("unknown region:").append(this).toString());
            return "JP";
        }
    }


    public Mobage()
    {
    }

    public static MarketCode getMarketCode()
    {
        return Platform.getInstance().getMarketCode();
    }

    public static String getSdkVersion()
    {
        return Platform.getInstance().getSdkVersion();
    }

    public static Mobage getInstance()
    {
    	if(mInstance == null)
    		mInstance = new Mobage();
    	return mInstance;
    }
    
    public static void loginWithUsernameAndPassword(String username, String password)
    {
        throw new RuntimeException("loginWithUsernameAndPassword not implemented");
    }

    public static Activity getCurrentActivity()
    {
        return currentActivity;
    }

    public static void setCurrentActivity(Activity activity)
    {
        currentActivity = activity;
        //setContext(activity);
    }

    public static void registerResources(int splashLayout, int closeButton, int coinBlue, int coinWhite)
    {
        Platform.getInstance().registerResources(splashLayout, closeButton, coinBlue, coinWhite);
    }

    public static void initialize(Region region, ServerMode serverMode, String consumerKey, String consumerSecret, String appId, Activity rootActivity)
    {
        setCurrentActivity(rootActivity);
        Platform.getInstance().initialize(rootActivity, appId, region, serverMode, consumerKey, consumerSecret);
    }

    public final void initialize(Activity activity, ServerMode serverMode, String appKey, String appVersion, String socialConsumerKey, String socialConsumerSecret)
    {
    	setMainActivity(activity);
    	initialize(activity, serverMode, appKey, appVersion, socialConsumerKey, socialConsumerSecret, null);
    	MLog.d("Mobage", (new StringBuilder("Initializing mobage NDK version: ")).append(getSDKVersion()).toString());
    }

    public static void initialize(ServerMode servermode, String appKey, String appVersion, String socialConsumerKey, String socialConsumerSecret) //a
            throws InvalidParameterException
    {
    	Mobage mobage = Mobage.getInstance();
    	try
    	{
    		mobage.serverMode = servermode;
    	}
    	catch (Exception ex)
    	{
    		MLog.e("Mobage", "Received exception", ex);
    		throw new InvalidParameterException(c("missing_server_mode"));
    	}
    	mConfigData = new ConfigData();
    	mConfigData.setAppKey(appKey);
    	mConfigData.setAppVersion(appVersion);
    	mConfigData.setConsumerKey(socialConsumerKey);
    	mConfigData.setConsumerSecret(socialConsumerSecret);
    	c(mContext);
    	ErrorMap.Init(mContext);
    	mSession.Init(getSession().getCredentials(), null, new BasicCookieStore());
    }

    public static void a(Context context)
    		throws InvalidParameterException
    {
    	mContext = context;
    	SharedPreferences reference;
    	reference = context.getSharedPreferences("initParams", 0);
    	int i1 = reference.getInt("serverMode", -1);
    	String s1 = reference.getString("appKey", null);
    	String s2 = reference.getString("appVersion", null);
    	String s3 = reference.getString("socialConsumerKey", null);
    	String s4 = reference.getString("socialConsumerSecret", null);
    	if(i1 == -1 || s1 == null || s2 == null || s3 == null || s4 == null)
    	{
    		throw new InvalidParameterException();
    	} else
    	{
    		initialize(ServerMode.values()[i1], s1, s2, s3, s4);
    		return;
    	}
    }

    
    public static void initialize(Context context, ServerMode servermode, String s1, String s2, String s3, String s4, ISgameHttpClientFactory f1)
    {
    	Mobage mobage = Mobage.getInstance();
    	android.content.SharedPreferences.Editor editor;
    	if(g.compareAndSet(false, true))
    	{
    		mContext = context;
    		String s8 = s4;
    		String s7 = s3;
    		String s6 = s2;
    		String s5 = s1;
    		ServerMode servermode1 = servermode;
    		Object obj;
    		editor = context.getApplicationContext().getSharedPreferences("initParams", 0).edit();
    		editor.putInt("serverMode", servermode1.ordinal());
    		editor.putString("appKey", s5);
    		editor.putString("appVersion", s6);
    		editor.putString("socialConsumerKey", s7);
    		editor.putString("socialConsumerSecret", s8);
    		editor.commit();
    		//e = new e();
    		try {
    			initialize(servermode, s1, s2, s3, s4);
			} catch (InvalidParameterException e1) {
				e1.printStackTrace();
			}
    		if(f1 != null)
    			mHttpClientFactory = f1;
    		else
    			getErrorInvalidSession();
    		GetUserToken(new Connection.IRequestHandler() {
				
				@Override
				public void onComplete() {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onError(Error error) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onComplete(SessionData sessiondata, Credentials credentials,
						CookieStore cookiestore) {
					// TODO Auto-generated method stub
					
				}
			});
        }
    }

    public static void GetUserToken(Connection.IRequestHandler handler) //a
    {
    	MLog.d("Mobage", "Attempting to login with existing session");
       	mConnection.GetUserToken(handler);
    }

    public final String getSDKVersion()
    {
        return Platform.getInstance().getSdkVersion(); //a(c("sdkVersion"));
    }

    public final String getAppVersion()
    {
        return getConfig().getAppVersion();
    }

    public final String getAppKey()
    {
        return getConfig().getAppKey();
    }

    public static void tick()
    {
    }

    public static void registerTick()
    {
    }

    public static void addPlatformListener(PlatformListener listener)
    {
        Platform.getInstance().addPlatformListener(listener);
    }

    public static void removePlatformListener(PlatformListener listener)
    {
        Platform.getInstance().removePlatformListener(listener);
    }

    public static void checkLoginStatus()
    {
        showLoginDialog();
    }

    public static void showLoginDialog()
    {
        try
        {
            Service.openLoginDialog(currentActivity, new IMobageHttpResponseHandler.OnLoginResponseHandler() {

                public void onComplete(User paramUser)
                {
                    MLog.e("Mobage", (new StringBuilder()).append("Successfully logged in as ").append(paramUser.getNickname()).toString());
                    for(Iterator ite = Platform.getInstance().mListeners.keySet().iterator(); ite.hasNext(); ((PlatformListener)ite.next()).onLoginComplete(paramUser.getId()));
                }

                public void onError(Error paramError)
                {
                    MLog.e("Mobage", (new StringBuilder()).append("Error logging in ").append(paramError.getDescription()).toString());
                    for(Iterator ite = Platform.getInstance().mListeners.keySet().iterator(); ite.hasNext(); ((PlatformListener)ite.next()).onLoginError(new Error(paramError.getCode(), paramError.getDescription())));
                }

                public void onCancel()
                {
                    for(Iterator ite = Platform.getInstance().mListeners.keySet().iterator(); ite.hasNext(); ((PlatformListener)ite.next()).onLoginCancel());
                }

            }
            		);
        }
        catch(Exception e)
        {
            MLog.e("Mobage", "json serialize error:", e);
        }
    }

    public static void showLogoutDialog(OnLogoutComplete onComplete)
    {
        logout();
        onComplete.onSuccess();
    }

    private static void logout() {
		// TODO Auto-generated method stub
		
	}

	public static void Login(Activity activity, HashMap hashmap, IMobageHttpResponseHandler.OnLoginResponseHandler onloginresponsehandler)
    {
        if(!Mobage.isInitialized())
        {
            onloginresponsehandler.onError(new Error(ErrorMap.MOBAGE_NOT_INITIALIZED));
            return;
        }
        if(!mLogin.compareAndSet(false, true))
        {
            onloginresponsehandler.onError(new Error(ErrorMap.LOGINREG_ALREADY_IN_FLIGHT));
            return;
        }
        else
        {
        	//Connection.IRequestHandlerWithError d1 = b(onloginresponsehandler);
        	// activity, hashmap, onloginresponsehandler, d1
            /*a(new Connection.IRequestHandlerWithError() {

                public final void onComplete()
                {
                }

                public final void onComplete(SessionData sessiondata, Credentials credentials, CookieStore cookiestore)
                {
                }

                public final void onError(Error error)
                {
                }
            }
            );*/
            return;
        }
    }

    public static void openLoginDialog(Activity activity, IMobageHttpResponseHandler.OnLoginResponseHandler onloginresponsehandler)
    {
        Login(activity, new HashMap(), onloginresponsehandler);
    }

    public static void openLoginDialog(Activity activity, HashMap hashmap, IMobageHttpResponseHandler.OnLoginResponseHandler onloginresponsehandler)
    {
        if(!Mobage.isInitialized())
        {
            onloginresponsehandler.onError(getErrorLogin());
            return;
        }
    }

    
    public static void hideSplashScreen()
    {
    }

    public static void onPause()
    {
        Platform.getInstance().pause();
    }

    public static void onResume()
    {
        Platform.getInstance().resume();
    }

    public static void onStop()
    {
        Platform.getInstance().stop();
    }

    public static void onDestroy()
    {
        Platform.getInstance().destroy();
    }

    public static void onRestart()
    {
        Platform.getInstance().restart();
    }

    public static boolean onBackPressed()
    {
        return Platform.getInstance().onBackPressed();
    }

    public static boolean isInitialized()
    {
        return Platform.getInstance().isReady();
    }

    public final ServerMode getServerMode()
    {
        return serverMode;
    }
    
    private static final String TAG = "Mobage";
    public static Activity currentActivity;
    static Mobage mInstance = null; 
    private ServerMode serverMode;

    static 
    {
    	mInstance = new Mobage();
    }

    public static void c(Context context)
    {
        mAppId = (new StringBuilder("AID:")).append(android.provider.Settings.Secure.getString(context.getContentResolver(), "android_id")).toString();
    }

    public static Credentials a()
    {
        Credentials credentials;
        (credentials = new Credentials()).setConsumerKey(mConfigData.getConsumerKey());
        credentials.setConsumerSecret(mConfigData.getConsumerSecret());
        return credentials;
    }

    static Activity setMainActivity(Activity activity)
    {
    	mRootActivity = activity;
        return activity;
    }

    public static String getUser() //f
    {
        return mConfigData.getCurrentUser();
    }

    public static String g()
    {
        return mConfigData.g();
    }

    public static String h()
    {
        return mConfigData.f();
    }

    public static final int c(String paramString)
    {
      return Resource.a(mContext, paramString);
    }

    public static final int d(String paramString)
    {
      return Resource.c(mContext, paramString);
    }

    public static final int e(String paramString)
    {
      return Resource.d(mContext, paramString);
    }

    public static final int f(String paramString)
    {
      return Resource.e(mContext, paramString);
    }

    public static final int g(String paramString)
    {
      return Resource.f(mContext, paramString);
    }

    public static final int h(String paramString)
    {
      return Resource.g(mContext, paramString);
    }


    // Private
    public static Session getSession()
    {
        return mSession;
    }

    public static String a(int i1)
    {
        if(mContext == null)
        {
            MLog.e("Mobage", "getString - context is null. Please call Mobage.initialize first");
            return "";
        } else
        {
            return mContext.getString(i1);
        }
    }

    public static boolean isLogin() //j
    {
        return Mobage.isInitialized() && getSession() != null && getSession().isInit() && getSession().getUser() != null;
    }

    public static boolean isConnected() //l
    {
        if(mContext == null)
        {
            MLog.e("Mobage", "hasNetwork - context is null. Please call Mobage.initialize first");
            return false;
        }
        Object obj;
        if((obj = mContext) == null)
            return false;
        if((obj = (ConnectivityManager)((Context) (obj)).getSystemService("connectivity")) == null)
            return false;
        if((obj = ((ConnectivityManager) (obj)).getActiveNetworkInfo()) == null)
            return false;
        else
            return ((NetworkInfo) (obj)).isConnected();
    }

    public static ISgameHttpClient createSgameHttpClient(String s1) //i
    {
        return mHttpClientFactory.createRequest(getSession().getCredentials(), getSession().getCookieStore(), s1, Mobage.getInstance().getAppKey(), Mobage.getInstance().getAppVersion(), Mobage.getInstance().getSDKVersion());
    }

    public static ISgameHttpClient createSgameHttpClient() //q
    {
        return createSgameHttpClient(null);
    }

    static ConfigData getConfig()
    {
        return mConfigData;
    }

    static AtomicBoolean y()
    {
        return g;
    }
    
    private static String mAppId; // c
    private static ConfigData mConfigData;
    //private static com.mobage.global.android.bank.iab.b e;
    private static Context mContext; //f
    private static AtomicBoolean g = new AtomicBoolean(false);
    private static final Session mSession = new Session(); //h
    //private static EventReporterSessionFactory i;
    private static ISgameHttpClientFactory mHttpClientFactory; //j
    //private static IMarketBillingServiceFactory k = new GoogleMarketBillingServiceFactory();
    //private static IEventReporterSession l;
    //private static IAnalyticsActivity m;
    private static Activity mRootActivity = null; //n
    private static List o = new CopyOnWriteArrayList();
    private static List p = new CopyOnWriteArrayList(); // p
    private static Map q = null;
    public static final AtomicBoolean mShowed = new AtomicBoolean(false); //a
    public static final AtomicBoolean mLogin = new AtomicBoolean(false); //b
    private static Connection mConnection = null; // r
}
